<?php 
namespace Admin\Controller;
use \Think\Controller;

class GoodsController extends Controller{

		protected $goodsModel = null;
		protected $catModel = null;
		protected $gtModel = null;
		public function __construct()
		{	
			parent::__construct();
			$this->goodsModel = D('goods');
			$this->catModel = D('category');
			$this->gtModel = D('goods_attr');
		}

	public function goods_list()
	{	
		$data = $this->catModel->getCats();
		$where = array();
		if(!empty(I('get.cat_id')))
		{
			$cat_id = $this->catModel->getSonID($data,I('get.cat_id'));

			if(empty($cat_id))
			{
				$where['cat_id'] =I('get.cat_id');
			}
			else
			{
				$cat_id = implode(',',$cat_id);
				$where['cat_id'] = array('in',$cat_id);
			}
		}
		if(!empty(I('get.brand_id')))
		$where ['brand_id'] = array('eq',I('get.brand_id'));
			
		$statue = I('get.state');
		echo $statue;
		if(!empty($statue) && in_array($statue,['is_new','is_hot','is_best']))
		{
				if($statue == 'is_best')
					$where['is_best'] = array('eq',1);

				if($statue == 'is_hot')
					$where['is_hot'] = array('eq',1);

				if($statue == 'is_new')
					$where['is_new'] = array('eq',1);
		}


		$saling = I('get.status');
			if(!empty($saling))
			{
			if($saling == 1)
				$where['is_on_sale'] = 1;

			if($saling == 0)
				$where['is_on_sale'] = 0;

			}

		$sele = I('get.select');

		if(!empty($sele))
			$where['goods_name'] = array('like',"%$sele%");

	
		//$datas = $this->goodsModel->->select();
		
		$count = $this->goodsModel->where($where)->count();  // 查询满足要求的总记录数
		
		$Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('next','下一页');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('end','最后一页');
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$datas = $this->goodsModel->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		//$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出

		$cats = $this->catModel->getTree($data);
		$brands = D('brand')->getBrand();
		$this->assign('brands',$brands);
		$this->assign('cats',$cats);
		$this->assign('datas',$datas);
		$this->display();
	}

	public function goods_add()
	{

			//判断提交方式
		if(IS_POST)
		{
			
			 $attr_ids = I('post.attr_id_list');
			 $attr_values = I('post.attr_value_list');
			 $attr_prices = I('post.attr_price_list');
			
			//接收数据
			if($this->goodsModel->create(I('post.')))
			{	
			$this->goodsModel->promote_start_time = strtotime(I('post.promote_start_time'));
			$this->goodsModel->promote_end_time = strtotime(I('post.promote_end_time'));
				
				if ($ret = $this->goodsModel->add()) {
					
					 if($ret)
					 {	
					 	foreach ($_POST['attr_id_list'] as $k => $v) 
					 	{
					 			
							$data['goods_id'] = $ret;
							$data['attr_id'] =$v;
							$data['attr_value'] = $attr_values[$k];
						 	$this->gtModel->add($data);
						 	
					 	} 
					 	$this->success('添加成功');	
					 	exit;
					}
					else
					{
						$this->error('添加失败');
						exit;
					}
				}
				else
				{
					$this->error($this->goodsModel->getError());
					exit;
				}
			}
			else
			{
				$this->error($this->goodsModel->getError());
				exit;
			}
		}

		$datas =$this->catModel->getCats();
		$cats = $this->catModel->getTree($datas);
		$types = D('goods_type')->getType();
		$brands = D('brand')->getBrand();		
		$this->assign('attrForm',$attrForm);
		$this->assign('types',$types);
		$this->assign('brands',$brands);
		$this->assign('cats',$cats);
		$this->display();
	}

	public function getStyle()
	{
			$attrForm = D('goods')->getAttrStyle(I('get.type_id'));

			echo $attrForm;
	}

	public function hsz()
	{
		$goods_id = I('get.goods_id');

		if($goods_id)
		{
			$data['is_delete'] = 1;
			$where['goods_id'] = $goods_id;
			
				$info = $this->goodsModel->where($where)->save($data);

				if(false !== $info)
				{
					echo '回收成功!';
				}

		}
	}


}

 ?>